package ch13;

import java.util.ArrayList;

// BankDatabase.java
// Represents the bank customers userID, customers account 
//and admin userID  information database 

public class BankDatabase {
	private ArrayList<Customer> customerList; // array of customer users
	private ArrayList<Admin> adminList; // array of admin users

	// no-argument BankDatabase constructor initializes accounts
	public BankDatabase() {
		customerList = new ArrayList<Customer>();
		adminList = new ArrayList<Admin>();

		// Creating admin users with attributes adminID, pin
		Admin admin1 = new Admin(1, 1);
		Admin admin2 = new Admin(2, 2);
		// Adding admin users to an ArrayList of admin users
		adminList.add(admin1);
		adminList.add(admin2);

		// Creating customer with attributes customerID, pin, CPR, first name,
		// last name
		Customer c1 = new Customer(55555, 55555, 3005908811L, "Muhamed", "Iseni");
		Customer c2 = new Customer(88888, 88888, 1706882369L, "Frederik", "Olsen");
		// Adding customer users to an ArrayList of customer
		customerList.add(c1);
		customerList.add(c2);

		// Adding accounts to costumers with attributes accounttype, available
		// and total balance, interest and overdraft
		c1.addAccounts(new Account("MainAccount", 500, 500, 1, 500));
		c1.addAccounts(new Account("LoanAccount", 1000, 1000, 1, 0));
		c1.addAccounts(new Account("CreditAccount", 1000, 1000, 1, 0));
		c2.addAccounts(new Account("MainAccount", 100000, 10000, 2, 0));
	} // end no-argument BankDatabase constructor
	
	// return customerList
	public ArrayList<Customer> getCustomerList() {
		return customerList;
	} // end method getCustomerList
	
	// retrieve Customer object containing specified CustomerID
	public Customer getCustomer(int customerID) {
   
		// loop through Costumer searching for matching CustomerID
		for (Customer currentCustomer : customerList) {
      
			// return current customer if match found
			if (currentCustomer.getCustomerID() == customerID)
				return currentCustomer;
		}// end for
      
		return null; // if no matching customer was found, return null
	} // end method getCustomer
	

	// determine whether user-specified customerID and PIN match
	// those of an customer in the database
	public boolean authenticateCustomer(int userCustomerID, int userPIN) {
   
		// attempt to retrieve the customer with the customerID number
		Customer costumerID = getCustomer(userCustomerID);

		// if customer exists, return result of Customer method validatePIN
		if (costumerID != null)
			return costumerID.validatePIN(userPIN);
		else
			return false; // customerID number not found, so return false
	} // end method authenticateCustomer

	// Credit an amount to Account with specified customerID number
	public void credit(int CustomerID, double amount) { 
   // credit amount from main account
	 	getCustomer(CustomerID).getAccounts().get(0).credit(amount); 
	} // end method credit

	// Debit an amount from Account with specified customerID number
	public void debit(int CustomerID, double amount) { 
   // credit amount from main account
		getCustomer(CustomerID).getAccounts().get(0).debit(amount); 
	} // end method debit

	// Adding accounts to costumers
	public void addAccounts(int customerID, Account _accounts) {
		getCustomer(customerID).addAccounts(_accounts);
	}// End method add Accounts

	// retrieve admin object containing specified AdminID
	public Admin getAdmin(int adminID) {
		// loop through Admin searching for matching AdminID
		for (Admin currentAdmin : adminList) {
			// return current Admin if match found
			if (currentAdmin.getAdminID() == adminID)
				return currentAdmin;
		} // end for

		return null; // if no matching customer was found, return null
	} // end method getCustomer

	// determine whether user-specified adminID and PIN match
	// those of an admin in the database
	public boolean authenticateAdmin(int userAdminID, int userPIN) {
		// attempt to retrieve the admin with the adminID number
		Admin adminID = getAdmin(userAdminID);

		// if admin exists, return result of admin method validatePIN
		if (adminID != null)
			return adminID.validatePIN(userPIN);
		else
			return false; // adminID number not found, so return false
	} // end method authenticateAdminUser

	// Adding accounts to costumers
	public void addCustomers(Customer theCustomers) {
		customerList.add(theCustomers);
	}// End method add Accounts

	// removing accounts to costumers
	public void removeCustomers(Customer theCustomers) {
		customerList.remove(theCustomers);
	}// End method remove Accounts

	public void setCustomersName(int customerID, String changeWhat,
			String changeTo) {
		switch (changeWhat) {
		case "firstName":
			getCustomer(customerID).setFirstName(changeTo);
			break;
		case "lastName":
			getCustomer(customerID).setLastName(changeTo);
			break;
		}
	}

} // end class BankDatabase